package controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import constant.Constant;
import entity.Chapter;
import exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.ChapterService;
import util.AjaxResult;
import util.DateUtil;
import util.ParameterUtil;
import vo.ChapterVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    ChapterService chapterService;

    //    “课程章节管理”页面加载章节信息
    @RequestMapping(value = "/loadChapter.do",produces = "text/plain;charset=utf-8")
    public String loadChapter(ChapterVo chapterVo,HttpServletResponse response) {
        System.out.println(chapterVo);

        //        解决乱码问题
        response.setCharacterEncoding("utf-8");

        PageInfo<Chapter> info = new PageInfo<>();
        try {
            info = chapterService.loadChapter(chapterVo);
            return JSON.toJSONString(AjaxResult.isOk(info));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return  JSON.toJSONString(AjaxResult.isFail(info));
    }



//    //加载课程评论
//    @RequestMapping("/sendChapterId.do")
//    public String loadComment(HttpServletRequest req) {
//        req.setAttribute("chapter_id",req.getParameter("chapter_id"));
//        return "/background/back_commentSet";//转发
//    }


    //加载课程详情页面
    @RequestMapping("/showChapterFront.do")
    public String loadCourseAndChapterInfo(Integer course_id, HttpSession session){
        List<Chapter> chapters = chapterService.selectChapterByCourseId(course_id);
        session.setAttribute("chapters",chapters);
        session.setAttribute("course_id",course_id);
        return "redirect:/front_course";//转发
    }

    // “课程章节管理”页面查询修改章节的信息
    @RequestMapping(value = "/showToModify.do",produces = "text/plain;charset=utf-8")
    public String showToModify(Integer id,HttpServletResponse response) {
        //        解决乱码问题
        response.setCharacterEncoding("utf-8");

        Chapter chapter = chapterService.showToModify(id);
        return JSON.toJSONString(AjaxResult.isOk(chapter));
    }

    // “课程章节管理”页面修改章节的信息
    @RequestMapping("/modifyChapter.do")
    public AjaxResult modifyChapter(Chapter chapter) {
        System.out.println(chapter);

        String msg = "修改失败！";
        try {
//            区分是修改信息还是修改状态
            if (ParameterUtil.isNull(chapter.getTitle()) && null == chapter.getStatus()) {
                return AjaxResult.isFail("请填写章节标题！");
            }
            if (chapterService.modifyChapter(chapter)) {
                return AjaxResult.isOk("修改成功！");
            }
        } catch (ServiceException e) {
            msg = e.getMessage();
        }
        System.out.println("修改完成**********");
        return AjaxResult.isFail(msg);
    }
    // “课程章节管理”页面添加章节信息
    @RequestMapping("/addChapter.do")
    public AjaxResult addChapter(Chapter chapter) {
        System.out.println("添加章节信息**********");
        System.out.println(chapter);

        String msg = "添加失败！";
        try {
            if (ParameterUtil.isNull(chapter.getTitle())) {
                return AjaxResult.isFail("请填写章节标题！");
            }
            if (chapterService.addChapter(chapter)) {
                return AjaxResult.isOk("添加章节信息成功！");
            }
        } catch (ServiceException e) {
            msg = e.getMessage();
        }
        System.out.println("添加完成**********");
        return AjaxResult.isFail(msg);
    }

//    前台查询全部章节
    @RequestMapping("/loadChapterByCourseId.do")
    @ResponseBody
    public List<Chapter> loadChapterByCourseId(Integer course_id){
        return chapterService.loadChapterByCourseId(course_id);
    }
}